/*
	Copyright (c) 2009-2011, Stefan Webb.

	This file is part of SimpleML. Please read the file "license.txt", 
	which contains the conditions under which this software is distributed.
*/

/*!
	\file criticalsection.cpp
	\author Stefan Webb
	\brief Definition of CriticalSection class methods.
*/

#include "stdafx.h"

/* Default constructor. */
CriticalSection::CriticalSection()
{
	// Initialize critical section. On Windows Vista and later, no error checking is required.
	if (!InitializeCriticalSectionAndSpinCount(&criticalSection_, 0x00000400)) {
		// TODO: More sophisticated error reporting using GetLastError for Windows XP etc.
		(error << "Could not create critical section.").exception();
	}
}

/* Default destructor. */
CriticalSection::~CriticalSection()
{
	// Release resources used by the critical section object.
    DeleteCriticalSection(&criticalSection_);
}

/* Enter critical section. */
void CriticalSection::enter()
{
	// Request ownership of the critical section.
    EnterCriticalSection(&criticalSection_);
}

/* Exit critical section. */
void CriticalSection::exit()
{
	// Release ownership of the critical section.
    LeaveCriticalSection(&criticalSection_);
}